<template>
  <div>
    <div>
      <div style="display: flex; justify-content: space-between">
        <div>
          <el-input
            clearable
            @clear="initRecogData"
            style="width: 300px; margin-right: 10px"
            type="text"
            v-model="recogContent"
            @keydown.enter.native="initRecogData"
            placeholder="请输入识别内容进行搜索..."
            prefix-icon="el-icon-search"
          ></el-input>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="initRecogData"
            >搜索</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click="showAdvanceSearch = !showAdvanceSearch"
          >
            <i
              :class="
                showAdvanceSearch
                  ? 'fa fa-angle-double-up'
                  : 'fa fa-angle-double-down'
              "
              style="margin-right: 5px"
              aria-hidden="true"
            ></i
            >高级搜索</el-button
          >
        </div>
        <div>
          <el-upload
            action="/recog/basic/import"
            style="display: inline-block; margin-right: 8px"
            :before-upload="beforeUpload"
            :disabled="importDataDisabled"
            :headers="headers"
            :show-file-list="false"
            :on-success="onSuccess"
            :on-error="onError"
          >
            <el-button
              type="success"
              size="small"
              :icon="importDataIcon"
              :disabled="importDataDisabled"
              >{{ importDataBtn }}</el-button
            >
          </el-upload>
          <el-button
            type="success"
            size="small"
            icon="el-icon-download"
            @click="exportRecog"
            >导出数据</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click="showAddRecog"
            >添加识别历史</el-button
          >
        </div>
      </div>
    </div>
    <el-collapse-transition>
      <div v-show="showAdvanceSearch" class="searchStyle">
        <el-row>
          <el-col :span="3">
            ID:
            <el-input
              v-model="recogSearch.id"
              clearable
              size="mini"
              style="width: 130px"
              placeholder="ID"
            >
            </el-input>
          </el-col>
          <el-col :span="3">
            渠道:
            <el-select
              v-model="recogSearch.way"
              size="mini"
              clearable
              style="width: 130px"
              placeholder="请选择图片获取途径"
            >
              <el-option
                v-for="item in ways"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="4">
            识别内容:
            <el-input
              clearable
              v-model="recogSearch.content"
              size="mini"
              style="width: 130px"
              placeholder="识别内容"
            >
            </el-input>
          </el-col>
          <el-col :span="7">
            日期范围:
            <el-date-picker
              v-model="recogSearch.beginDateScope"
              clearable
              type="daterange"
              unlink-panels
              value-format="yyyy-MM-dd"
              size="mini"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-col>
          <el-col :span="3">
            <el-select
              v-model="recogSearch.user_id"
              clearable
              size="mini"
              style="width: 130px"
              placeholder="选择发起的用户"
            >
              <el-option
                v-for="item in users"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="2">
            <el-button
              size="mini"
              icon="el-icon-search"
              type="primary"
              @click="initRecogData('advance')"
              >搜索</el-button
            >
          </el-col>
        </el-row>
      </div>
    </el-collapse-transition>
    <div style="margin-top: 10px">
      <el-table
        :data="recogData"
        show-header
        border
        v-loading="loading"
        element-loading-text="数据加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        style="width: 100%"
      >
        <el-table-column type="selection" width="35"> </el-table-column>
        <el-table-column prop="id" fixed label="#" align="center" width="50">
        </el-table-column>
        <el-table-column label="途径" align="center" width="60">
          <template slot-scope="scope">
            <img
              v-if="scope.row.way == 'camera'"
              src="https://img0.baidu.com/it/u=2641309264,2467450606&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
              width="40"
              height="40"
            />
            <img
              v-if="scope.row.way == 'picture'"
              src="https://img0.baidu.com/it/u=464037393,2385019681&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
              width="40"
              height="40"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="识别内容"
          align="center"
          width="835"
        >
        </el-table-column>
        <el-table-column
          prop="recog_time"
          label="识别时间"
          align="center"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="recog_ip"
          label="识别发起IP"
          align="center"
          width="150"
        >
        </el-table-column>
        <el-table-column
          prop="user_id"
          label="发起用户ID"
          align="center"
          width="120"
        >
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditView(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteRecog(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="display: flex; justify-content: flex-end"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20, 25, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="400px">
      <el-form
        :model="recog"
        label-width="100px"
        :rules="recogRule"
        ref="recogForm"
      >
        <el-form-item label="途径：" prop="way">
          <el-select
            v-model="recog.way"
            clearable
            style="width: 380px"
            placeholder="请选择图片获取途径"
          >
            <el-option
              v-for="item in ways"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="识别内容：" prop="content">
          <el-input
            v-model="recog.content"
            clearable
            type="textarea"
            maxlength="1000"
            show-word-limit
            resize="vertical"
            placeholder="请输入识别内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="识别时间：" prop="recog_time">
          <el-date-picker
            v-model="recog.recog_time"
            type="datetime"
            style="width: 350px"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请输入识别时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="IP来源：" prop="recog_ip">
          <el-input
            v-model="recog.recog_ip"
            clearable
            placeholder="请输入IP来源"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户ID：" prop="user_id">
          <el-select
            v-model="recog.user_id"
            clearable
            style="width: 380px"
            placeholder="请输入用户ID"
          >
            <el-option
              v-for="item in users"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addRecog">提 交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "RecogHistory",
  data() {
    return {
      users: [],
      ways: [
        { label: "图片", value: "picture" },
        { label: "相机", value: "camera" },
      ],
      recogData: [],
      loading: false,
      defaultProps: {
        children: "children",
        label: "name",
      },
      visible: false,
      visible2: false,
      showAdvanceSearch: false,
      recogSearch: {
        id: null,
        way: null,
        content: null,
        beginDateScope: null,
        user_id: null,
      },
      recog: {
        id: null,
        way: null,
        content: null,
        recog_time: null,
        recog_ip: null,
        user_id: null,
      },
      recogRule: {
        content: [
          { required: true, message: "请输入识别内容", trigger: "blur" },
        ],
        recog_time: [
          {
            required: true,
            message: "请选择识别时间",
            trigger: "blur",
          },
        ],
        recog_ip: [
          {
            required: true,
            message: "请输入识别历史发起的IP",
            trigger: "blur",
          },
        ],
        user_id: [
          {
            required: true,
            message: "请选择发起用户",
            trigger: "blur",
          },
        ],
      },
      currentPage: 1,
      size: 5,
      total: 0,
      recogContent: "",
      dialogVisible: false,
      title: "",
      importDataIcon: "el-icon-upload2",
      importDataBtn: "导入数据",
      importDataDisabled: false,
      headers: {
        Authorization: window.sessionStorage.getItem("tokenStr"),
      },
    };
  },
  mounted() {
    this.initRecogData();
    this.initUserDate();
  },
  methods: {
    initUserDate() {
      this.postRequest("/user/basic/getSelections").then((res) => {
        this.loading = true;
        if (res) {
          this.users = res.obj;
        }
        this.loading = false;
      });
    },
    showEditView(data) {
      this.dialogVisible = true;
      this.title = "编辑识别历史信息";
      this.recog = data;
    },
    deleteRecog(data) {
      this.$confirm(
        "此操作将永久删除【" + data.content + "】, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.deleteRequest("/recog/basic/delete/" + data.id).then((res) => {
            if (res) {
              this.initRecogData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "warning",
            message: "取消删除",
          });
        });
    },
    addRecog() {
      this.$refs["recogForm"].validate((valid) => {
        if (valid) {
          if (this.title === "编辑识别历史信息") {
            this.putRequest("/recog/basic/update", this.recog).then((res) => {
              if (res) {
                this.dialogVisible = false;
                this.initRecogData();
              }
            });
          } else {
            this.postRequest("/recog/basic/add", this.recog).then((res) => {
              if (res) {
                this.dialogVisible = false;
                this.initRecogData();
              }
            });
          }
        } else {
          this.$message.warning("请完善表单信息！");
        }
      });
    },
    getMaxId() {
      this.getRequest("/recog/basic/maxId").then((res) => {
        if (res) {
          this.recog.id = res.obj;
        }
      });
    },
    beforeUpload() {
      this.importDataBtn = "正在导入";
      this.importDataIcon = "el-icon-loading";
      this.importDataDisabled = true;
    },
    onSuccess() {
      this.importDataBtn = "导入数据";
      this.importDataIcon = "el-icon-upload2";
      this.importDataDisabled = false;
      this.initRecogData();
      this.$message.success("导入成功!");
    },
    onError() {
      this.importDataBtn = "导入数据";
      this.importDataIcon = "el-icon-upload2";
      this.importDataDisabled = false;
    },
    exportRecog() {
      this.downloadRequest("/recog/basic/export");
    },
    showAddRecog() {
      this.getMaxId();
      this.title = "添加识别历史";
      this.recog = {};
      this.dialogVisible = !this.dialogVisible;
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.initRecogData();
    },
    handleSizeChange(size) {
      this.size = size;
      this.initRecogData();
    },
    initRecogData(type) {
      let url =
        "/recog/basic/listAll/?currentPage=" +
        this.currentPage +
        "&size=" +
        this.size;
      if (type && type === "advance") {
        if (this.recogSearch.id) {
          url += "&id=" + this.recogSearch.id;
        }
        if (this.recogSearch.way) {
          url += "&way=" + this.recogSearch.way;
        }
        if (this.recogSearch.content) {
          url += "&content=" + this.recogSearch.content;
        }
        if (this.recogSearch.beginDateScope) {
          url += "&dateScope=" + this.recogSearch.beginDateScope;
        }
        if (this.recogSearch.user_id) {
          url += "&user_id=" + this.recogSearch.user_id;
        }
      } else {
        if (this.recogContent) {
          if (this.recogContent === "") {
          } else {
            url += "&content=" + this.recogContent;
          }
        }
      }
      this.postRequest(url).then((res) => {
        this.loading = true;
        if (res) {
          this.loading = false;
          this.recogData = res.data;
          this.total = res.total;
        }
      });
    },
  },
};
</script>

<style scoped>
.deptStyle {
  width: 150px;
  height: 28px;
  font-size: 13px;
  border: 1px solid #dedede;
  display: inline-flex;
  border-radius: 5px;
  box-sizing: border-box;
  padding-left: 15px;
  cursor: pointer;
  align-items: center;
}
.deptSearch {
  width: 130px;
  height: 28px;
  font-size: 13px;
  display: inline-flex;
  border: 1px solid #dedede;
  border-radius: 5px;
  box-sizing: border-box;
  padding-left: 15px;
  cursor: pointer;
  align-items: center;
}
.searchStyle {
  border: 1px solid #409eff;
  border-radius: 5px;
  box-sizing: border-box;
  margin: 10px;
  padding: 5px;
}
</style>
